package blog.web.wirte;

import blog.sys.entity.ReleaseContent;
import blog.sys.entity.SysFile;
import blog.sys.service.ReleaseContentService;
import blog.sys.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import pub.spring.web.mvc.ActionResult;
import pub.spring.web.mvc.model.Model;
import pub.spring.web.mvc.view.JsonView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 2017/2/14.
 */
@Controller
public class MainController {

    @RequestMapping
    public ActionResult select_map(Model model) {
        return ActionResult.ok();
    }

    @RequestMapping
    public ActionResult onSaveContent(ReleaseContent releaseContent) {
        ActionResult result = ActionResult.ok();
        try {
            if (releaseContent != null) {
                releaseContent.setUserId(releaseContent.getUserId());
                releaseContent.setType(1);
                releaseContent.setStatus(1);
                releaseContent.setCreateTime(new Date());
                releaseContent.setTitle(releaseContent.getTitle());

                Integer id = releaseContentService.onSaveContent(releaseContent);
                result.setData(id);
                result.setMsg("发送成功!");
                return result;
            } else {
                result.setMsg("请输入内容!");
            }

        } catch (Exception e) {
            result.error("发送失败!");
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 保存图片
     *
     * @param file
     * @return
     */
    @RequestMapping
    @ModelAttribute(JsonView.directResult)
    public Map<String, String> onSavePictrue(@RequestParam(value = "file", required = false) MultipartFile file) {
//        ActionResult result = ActionResult.ok();
        Map<String, String> map = new HashMap<>();
        String url = "";
        String fileType = releaseContentService.returnFileType(file.getOriginalFilename());
        try {
            SysFile sysFile = new SysFile();
            sysFile.setCreateTime(new Date());
            sysFile.setSeq(0);
            sysFile.setName(file.getOriginalFilename());
            sysFile.setRelId(0);
            sysFile.setRelTable("t_release_content");
            sysFile.setContent(file.getBytes());
            sysFile.setSize((int) file.getSize());
            sysFile.setType(fileType == "图片" ? 3 : fileType == "文档" ? 0 : fileType == "视频" ? 2 : fileType == "音乐" ? 1 : 4);
            sysFile.setContentType("image/*");

            url = releaseContentService.saveFile(sysFile);

            map.put("link", url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }


    @Autowired
    private ReleaseContentService releaseContentService;

    @Autowired
    private SysFileService sysFileService;

}
